package com.rain.cy.framework.entity;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.Optional;


/**
 * 基础的Controller
 *
 * @author yu_chen
 * @date 2018年5月30日15:9:54
 */
@Slf4j
public class BaseController {

    public static final String USER_ID_KEY = "userId";

    public static final String IS_DOCTOR_KEY = "isDoctor";

    protected HttpServletRequest request() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes == null ? null : servletRequestAttributes.getRequest();
    }

    protected HttpServletResponse response() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes == null ? null : servletRequestAttributes.getResponse();
    }

    /**
     * 获取当前登录的用户ID
     *
     * @return Long UserId
     */
    protected Integer getCurrentUserId() {
        String userId = request().getHeader(USER_ID_KEY);
        return Integer.parseInt(userId);

    }

}
